package com.laicunba.event.listener;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.WithdrawToBankCardEvent;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.TemplateDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.pojo.WithdrawDO;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.MobileMessageService;
import com.laicunba.service.TemplateService;
import com.laicunba.service.UserService;
import com.laicunba.util.StringUtil;

@Component
public class WithdrawToBankCardEventListener implements ApplicationListener<WithdrawToBankCardEvent> {
	@Autowired
	private TemplateService templateService;
	@Autowired
	private MobileMessageService mobileMessageService;
	@Autowired
	private UserService userService;
	@Autowired
	private FinancingTargetService financingTargetService;

	private String shorNo(String cardNo) {
		if (StringUtil.isEmpty(cardNo)) {
			return "";
		}
		if (cardNo.length() < 10) {
			return cardNo;
		}
		return cardNo.substring(cardNo.length() - 5);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void onApplicationEvent(WithdrawToBankCardEvent event) {
		if (event.getSource() instanceof WithdrawDO) {
			WithdrawDO withdrawDO = (com.laicunba.pojo.WithdrawDO) event.getSource();
			TemplateDO tmp = templateService.findByCodeWidthDefault("SCB_WITHDRAW");

			Map param = new HashMap();
			param.put("BankName", withdrawDO.getBankName());
			param.put("CardNo", shorNo(withdrawDO.getBankCardNo()));
			param.put("Amount", withdrawDO.getWithdrawPrincipal().add(withdrawDO.getWithdrawProfit() != null ? withdrawDO.getWithdrawProfit() : BigDecimal.ZERO));
			String msg = templateService.simpleMerge(tmp.getContent(), param);
			UserDO user = userService.get(UserDO.class, withdrawDO.getUserId());
			mobileMessageService.sendMsgAsync(user.getMobile(), msg);
		} else if (event.getSource() instanceof FinancingTargetBidDO) {
			FinancingTargetBidDO bid = (FinancingTargetBidDO) event.getSource();
			if ("ACTIVITY".equalsIgnoreCase(bid.getPayMark())) {
				TemplateDO tmp = templateService.findByCodeWidthDefault("ACTIVITY_PROFIT2SCB");
				// 活动,发短信提醒,并且自动买购随存宝
				Map param = new HashMap();
				param.put("Principal", bid.getWithdrawPrincipal());
				param.put("Profit", bid.getWithdrawProfit());
				String msg = templateService.simpleMerge(tmp.getContent(), param);
				UserDO user = userService.get(UserDO.class, bid.getBidUserId());
				mobileMessageService.sendMsgAsync(user.getMobile(), msg);
			} else {

				TemplateDO tmp = templateService.findByCodeWidthDefault("CPB_WITHDRAW");
				FinancingTargetDO target = financingTargetService.get(FinancingTargetDO.class, bid.getFinancingTargetId());

				Map param = new HashMap();
				param.put("targetName", target.getName());
				param.put("BankName", bid.getBankName());
				param.put("CardNo", shorNo(bid.getBankCardNo()));
				param.put("Principal", bid.getWithdrawPrincipal());
				param.put("Profit", bid.getWithdrawProfit());
				param.put("Amount", bid.getWithdrawPrincipal().add(bid.getWithdrawProfit() != null ? bid.getWithdrawProfit() : BigDecimal.ZERO));
				String msg = templateService.simpleMerge(tmp.getContent(), param);
				UserDO user = userService.get(UserDO.class, bid.getBidUserId());
				mobileMessageService.sendMsgAsync(user.getMobile(), msg);
			}
		}
	}

}
